package com.zytech.zspider.eventbus;

import com.google.common.base.Objects;
import com.zytech.modules.lang.Requires;

import java.io.Serializable;

/**
 * @author siuming
 */
public class EventEnvelope implements Serializable {
    private static final long serialVersionUID = -6010868152769943657L;

    private final long serialNumber;
    private final long timestamp;
    private final Event event;

    /**
     * @param serialNumber
     * @param timestamp
     * @param event
     */
    EventEnvelope(long serialNumber, long timestamp, Event event) {
        this.serialNumber = serialNumber;
        this.timestamp = timestamp;
        this.event = event;
    }

    public long getSerialNumber() {
        return serialNumber;
    }

    public long getTimestamp() {
        return timestamp;
    }

    public Event getEvent() {
        return event;
    }

    @Override
    public String toString() {
        return Objects.toStringHelper(this)
                .add("serialNumber", getSerialNumber())
                .add("timestamp", getTimestamp())
                .add("event", getEvent())
                .toString();
    }

    /**
     * @param event
     * @return
     */
    public static EventEnvelope of(Event event) {
        return of(System.currentTimeMillis(), event);
    }

    /**
     * @param serialNumber
     * @param event
     * @return
     */
    public static EventEnvelope of(long serialNumber, Event event) {
        return of(serialNumber, System.currentTimeMillis(), event);
    }

    /**
     * @param serialNumber
     * @param timestamp
     * @param event
     * @return
     */
    public static EventEnvelope of(long serialNumber, long timestamp, Event event) {
        Requires.notNull(event, "event must not be null.");
        return new EventEnvelope(serialNumber, timestamp, event);
    }
}
